extends Node

enum Action {LEFT, RIGHT, UP, DOWN, SHOOT, PASS}

const ACTIONS_MAP : Dictionary = {
	Player.ControlScheme.P1: {
		Action.LEFT: "left",
		Action.RIGHT: "right",
		Action.UP: "up",
		Action.DOWN: "down",
		Action.SHOOT: "shoot",
		Action.PASS: "pass",
	},
	Player.ControlScheme.P2: {
		Action.LEFT: "Kp 4",
		Action.RIGHT: "Kp 6",
		Action.UP: "Kp 8",
		Action.DOWN: "Kp 5",
		Action.SHOOT: "Kp 7",
		Action.PASS: "Kp 9",
	}
}

var PRESS_MAP : Dictionary = {
	Action.LEFT: 	{
						"press" : false,
						"time" : 0,
					},
	Action.RIGHT: 	{
						"press" : false,
						"time" : 0,
					},
	Action.UP:		{
						"press" : false,
						"time" : 0,
					},
	Action.DOWN: 	{
						"press" : false,
						"time" : 0,
					},
}

func put_press_map(map : Dictionary) -> void:
	if Input.is_action_just_pressed(map[Action.LEFT]):
		PRESS_MAP[Action.LEFT].set("press", true)
		PRESS_MAP[Action.LEFT].set("time", Time.get_ticks_msec())
	if Input.is_action_just_pressed(map[Action.RIGHT]):
		PRESS_MAP[Action.RIGHT].set("press", true)
		PRESS_MAP[Action.RIGHT].set("time", Time.get_ticks_msec())
	if Input.is_action_just_pressed(map[Action.UP]):
		PRESS_MAP[Action.UP].set("press", true)
		PRESS_MAP[Action.UP].set("time", Time.get_ticks_msec())
	if Input.is_action_just_pressed(map[Action.DOWN]):
		PRESS_MAP[Action.DOWN].set("press", true)
		PRESS_MAP[Action.DOWN].set("time", Time.get_ticks_msec())

	if Input.is_action_just_released(map[Action.LEFT]):
		PRESS_MAP[Action.LEFT].set("press", false)
		PRESS_MAP[Action.LEFT].set("time", 0)
	if Input.is_action_just_released(map[Action.RIGHT]):
		PRESS_MAP[Action.RIGHT].set("press", false)
		PRESS_MAP[Action.RIGHT].set("time", 0)
	if Input.is_action_just_released(map[Action.UP]):
		PRESS_MAP[Action.UP].set("press", false)
		PRESS_MAP[Action.UP].set("time", 0)
	if Input.is_action_just_released(map[Action.DOWN]):
		PRESS_MAP[Action.DOWN].set("press", false)
		PRESS_MAP[Action.DOWN].set("time", 0)

func get_input_from_map() -> Vector2:
	var last_velocity = Vector2.ZERO
	if PRESS_MAP[Action.LEFT].get("press") and PRESS_MAP[Action.RIGHT].get("press"):
		last_velocity += Vector2.LEFT if PRESS_MAP[Action.LEFT].get("time") > PRESS_MAP[Action.RIGHT].get("time") else Vector2.RIGHT
	elif PRESS_MAP[Action.LEFT].get("press"):
		last_velocity += Vector2.LEFT
	elif PRESS_MAP[Action.RIGHT].get("press"):
		last_velocity += Vector2.RIGHT

	if PRESS_MAP[Action.UP].get("press") and PRESS_MAP[Action.DOWN].get("press"):
		last_velocity += Vector2.UP if PRESS_MAP[Action.UP].get("time") > PRESS_MAP[Action.DOWN].get("time") else Vector2.DOWN
	elif PRESS_MAP[Action.UP].get("press"):
		last_velocity += Vector2.UP
	elif PRESS_MAP[Action.DOWN].get("press"):
		last_velocity += Vector2.DOWN

	return last_velocity

func get_input_vector(scheme: Player.ControlScheme) -> Vector2:
	var map : Dictionary = ACTIONS_MAP[scheme]
	var velocity = Input.get_vector(map[Action.LEFT],map[Action.RIGHT],map[Action.UP],map[Action.DOWN],)

	put_press_map(map)
	if velocity == Vector2.ZERO:
		if Input.is_anything_pressed():
			velocity += get_input_from_map()

	return velocity.normalized()

func is_action_pressed(scheme: Player.ControlScheme, action: Action) -> bool:
	return Input.is_action_pressed(ACTIONS_MAP[scheme][action])

func is_action_just_pressed(scheme: Player.ControlScheme, action: Action) -> bool:
	return Input.is_action_just_pressed(ACTIONS_MAP[scheme][action])

func is_action_just_released(scheme: Player.ControlScheme, action: Action) -> bool:
	return Input.is_action_just_released(ACTIONS_MAP[scheme][action])
